package com.sikaryofficial.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sikaryofficial.backend.annotation.TenantFilter;
import com.sikaryofficial.backend.constant.ReportTypeEnum;
import com.sikaryofficial.backend.domain.dto.SysDictDataDTO;
import com.sikaryofficial.backend.domain.dto.req.ReportAcceptReq;
import com.sikaryofficial.backend.domain.dto.req.ReportListReq;
import com.sikaryofficial.backend.domain.dto.req.ReportReq;
import com.sikaryofficial.backend.domain.dto.resp.ReportDTO;
import com.sikaryofficial.backend.manager.UserReportManager;
import com.sikaryofficial.common.core.utils.HiResultUtil;
import com.sikaryofficial.common.core.utils.PageUtil;
import com.sikaryofficial.common.core.web.controller.BaseController;
import com.sikaryofficial.common.core.web.domain.HiResult;
import com.sikaryofficial.common.core.web.domain.PageResult;
import com.sikaryofficial.common.log.annotation.Log;
import com.sikaryofficial.common.log.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 举报Controller
 *
 * @author qinjinyuan
 * @date 2023-11-08
 */
@RestController
@RequestMapping("/user/report")
@Api(tags = "用户举报")
public class UserReportController extends BaseController {
    @Autowired
    private UserReportManager reportManager;
    /**
     * 新增举报
     */
    @Log(title = "举报", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增举报")
    public HiResult<Boolean> add(@Validated @RequestBody ReportReq report) {
        return HiResultUtil.success(reportManager.reportSave(report));
    }
    /**
     * 查询举报类型
     */
    @PostMapping("/reportType")
    @ApiOperation("查询举报类型")
    public HiResult<List<SysDictDataDTO>> getReportType() {
        ReportTypeEnum[] reportTypeEnums = ReportTypeEnum.values();
        List<SysDictDataDTO> result = new ArrayList<>();
        for (ReportTypeEnum reportTypeEnum : reportTypeEnums) {
            SysDictDataDTO sysDictDataDTO = new SysDictDataDTO();
            result.add(sysDictDataDTO);
            sysDictDataDTO.setDictLabel(reportTypeEnum.getDescription());
            sysDictDataDTO.setDictValue(reportTypeEnum.getReportType() + "");
            sysDictDataDTO.setDictType("report_type");
        }
        return HiResultUtil.success(result);
    }
    /**
     * 查询举报分页列表
     */
    @PostMapping("/list")
    @ApiOperation("查询举报列表-个人中心")
    public PageResult<ReportDTO> list(@Validated @RequestBody ReportListReq report) {
        IPage<ReportDTO> result = reportManager.ownerList(report);
        return PageUtil.success(result.getRecords(), result.getTotal());
    }
    /**
     * 查询工单分页列表
     */
    @PostMapping("/listPage")
    @ApiOperation("仿品举报列表-后台管理")
    @TenantFilter
    public PageResult<ReportDTO> listPage(@Validated @RequestBody ReportListReq req) {
        IPage<ReportDTO> result = reportManager.listPage(req);
        return PageUtil.success(result.getRecords(), result.getTotal());
    }
    /**
     * 获取举报详细信息
     */
    @GetMapping(value = "/{reportId}")
    @ApiOperation("获取举报详细信息")
    @TenantFilter
    public HiResult<ReportDTO> getInfo(@PathVariable("reportId") Long reportId) {
        return HiResultUtil.success(reportManager.getInfo(reportId));
    }

    /**
     * 获取举报详细信息
     */
    @GetMapping(value = "/delete/{reportId}")
    @ApiOperation("删除举报详细信息")
    @TenantFilter
    public HiResult<Boolean> removeInfo(@PathVariable("reportId") Long reportId) {
        return HiResultUtil.success(reportManager.removeInfo(reportId));
    }

    @Log(title = "举报受理", businessType = BusinessType.UPDATE)
    @PostMapping("/accept")
    @ApiOperation("举报受理")
    @TenantFilter
    public HiResult<Boolean> accept(@Validated @RequestBody ReportAcceptReq req) {
        return HiResultUtil.success(reportManager.accept(req));
    }

    /**
     * 自动处理举报
     */
    @GetMapping("/autoProcess")
    @ApiOperation("自动处理举报")
    public HiResult<Boolean> autoProcess() {
        return HiResultUtil.success(reportManager.autoProcess());
    }
}
